package com.tdkj.action.backstage.risevalue;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.tdkj.action.BaseAction;
import com.tdkj.model.common.BeanType;
import com.tdkj.model.risevalue.BeanAdvert;
import com.tdkj.model.risevalue.BeanAdvertAuctionBidInfo;
import com.tdkj.model.risevalue.BeanAdvertAuctionInfo;
import com.tdkj.model.risevalue.BeanAuctionRule;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.risevalue.IBeanAdvertAuctionBidService;
import com.tdkj.service.risevalue.IBeanAdvertAuctionService;
import com.tdkj.service.risevalue.IBeanAdvertService;
import com.tdkj.service.risevalue.IBeanAuctionRuleService;


public class AdvertAction extends BaseAction {
    private Long id;
    private Long aid;
    private Long bid;
    private Long rid;
    private int startprice;//起价格
    private String currency;//币种
    private int timeinterval;//时间间隔
    private String unit;//时间单位：小时和分钟
    private String fullId;
    private String message;
    private BeanAdvert advert;
    private List<BeanAuctionRule> ruleList=new ArrayList<BeanAuctionRule>();
    private BeanAdvertAuctionInfo auctioninfo;
	private IBeanAdvertService advertService;
	private IBeanAdvertAuctionService auctionService;
	private IBeanAdvertAuctionBidService auctionBidService;
	private IBeanAuctionRuleService ruleService;
	private String[] ids;
	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}
	public String getCurrency() {
		return currency;
	}

	public void setCurrency(String currency) {
		this.currency = currency;
	}
	public int getTimeinterval() {
		return timeinterval;
	}

	public void setTimeinterval(int timeinterval) {
		this.timeinterval = timeinterval;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public int getStartprice() {
		return startprice;
	}
	public void setStartprice(int startprice) {
		this.startprice = startprice;
	}
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Long getRid() {
		return rid;
	}

	public void setRid(Long rid) {
		this.rid = rid;
	}

	public String getFullId() {
		return fullId;
	}
	public void setFullId(String fullId) {
		this.fullId = fullId;
	}
	public Long getAid() {
		return aid;
	}
	public void setAid(Long aid) {
		this.aid = aid;
	}
	public Long getBid() {
		return bid;
	}
	public void setBid(Long bid) {
		this.bid = bid;
	}
	public BeanAdvert getAdvert() {
		return advert;
	}
	public void setAdvert(BeanAdvert advert) {
		this.advert = advert;
	}
	public BeanAdvertAuctionInfo getAuctioninfo() {
		return auctioninfo;
	}
	public void setAuctioninfo(BeanAdvertAuctionInfo auctioninfo) {
		this.auctioninfo = auctioninfo;
	}
	public List<BeanAuctionRule> getRuleList() {
		return ruleList;
	}

	public void setRuleList(List<BeanAuctionRule> ruleList) {
		this.ruleList = ruleList;
	}

	public IBeanAdvertService getAdvertService() {
		return advertService;
	}
	@Resource
	public void setAdvertService(IBeanAdvertService advertService) {
		this.advertService = advertService;
	}
	public IBeanAdvertAuctionService getAuctionService() {
		return auctionService;
	}
	@Resource
	public void setAuctionService(IBeanAdvertAuctionService auctionService) {
		this.auctionService = auctionService;
	}
	public IBeanAdvertAuctionBidService getAuctionBidService() {
		return auctionBidService;
	}
	@Resource
	public void setAuctionBidService(IBeanAdvertAuctionBidService auctionBidService) {
		this.auctionBidService = auctionBidService;
	}
	public IBeanAuctionRuleService getRuleService() {
		return ruleService;
	}
	@Resource
	public void setRuleService(IBeanAuctionRuleService ruleService) {
		this.ruleService = ruleService;
	}

	public String findAll(){
		return "main";
	}
	public String findList(){
		if(fullId!=null){
			String hql = "select bean from " + BeanAdvert.class.getName() +" as bean, " + BeanType.class.getName() + " as type "
			+ " where bean.pageid=type.id and (type.fullId='"+fullId+"' or type.fullId like '"+fullId+"-%')" 
			+ " order by pagename desc";
			Query query = this.createQuery(hql);
			this.queryByPage(query, "advertAction!findList.action", 10);
		}else{
			Criteria criteria = this.createCriteria(BeanAdvert.class);
			criteria.addOrder(Order.desc("name"));// 对状态进行排序
			this.queryByPage(criteria, "advertAction!findList.action", 10);
		}
		return "list";
	}
	public String findAddPage(){
		return "addPage";
	}
	public String findEditPage(){
		advert=advertService.findById(this.getId(), BeanAdvert.class);
		return "editPage";
	}
	public String findAdvertDetail(){
		advert=advertService.findById(this.getId(), BeanAdvert.class);
		return "detailPage";
	}
	//竞价明细，按轮次
	public String findAuctionDetail(){
		auctioninfo=auctionService.findById(this.getAid(), BeanAdvertAuctionInfo.class);
		Criteria criteria = this.createCriteria(BeanAdvertAuctionBidInfo.class);
		criteria.add(Restrictions.eq("auctioninfo",auctioninfo));
		criteria.addOrder(Order.desc("auctiontime"));// 对状态进行排序
		this.queryByPage(criteria, "advertAction!findAuctionDetail.action", 10);
		return "auctionDetailPage";
	}
	
	//获取竞价轮次的添加界面
	public String findAuctionAddPage(){
		id=this.getId();
		ruleList=ruleService.findAll(BeanAuctionRule.class);
		
		//提供默认值
		this.setTimeinterval(1);
		setStartprice(0);
//		if(null==auctioninfo)
//			auctioninfo=new BeanAdvertAuctionInfo();
//		auctioninfo.setStarttime(new Timestamp(System.currentTimeMillis()));
//		auctioninfo.setEndtime(new Timestamp(System.currentTimeMillis()));
		//提供默认值 end
		return "auctionAddPage";
	}
	public String addAdvert(){
		this.getAdvert().setStatus("空闲");
		advertService.save(this.getAdvert());
		return findAddPage();
	}
	//添加广告位的竞价轮次
	public String addAdvertAuction(){
		//BeanAuctionRule r=ruleService.findById(this.getRid(), BeanAuctionRule.class);
		BeanAdvert bean=advertService.findById(this.getId(), BeanAdvert.class);
			bean.setStatus("开始竞价");
			advertService.saveOrUpdate(bean, BeanAdvert.class);
			//BeanUser u=advertService.findById(4l, BeanUser.class);
			BeanAdvertAuctionInfo a=this.getAuctioninfo();
			a.setAdvert(bean);
			//a.setUser(u);
			//a.setAuctionrule(r);
			a.setStatus("开始竞价");
			a.setPaystatus("未支付");
			a.setWorkingstatus("空闲");
			if(this.getUnit()!=null){a.setUnit(this.getUnit());}
			if(this.getStartprice()!=0){a.setStartprice(this.getStartprice());}
			//if(this.getCurrency()!=null){a.setCurrency(this.getCurrency());}
			a.setCurrency("金币");
			if(this.getTimeinterval()!=0){a.setTimeinterval(this.getTimeinterval());}
			auctionService.save(a);
		return findAdvertDetail();
	}
	
	//设置为竞价成功
	public String updateAdvertAuction(){
		aid=this.getAid();
		BeanAdvertAuctionInfo auctioninfo=advertService.findById(this.getAid(), BeanAdvertAuctionInfo.class);
		BeanAdvertAuctionBidInfo bidinfo=auctionBidService.findById(this.getBid(), BeanAdvertAuctionBidInfo.class);
		auctioninfo.setUser(bidinfo.getUser());
		auctioninfo.setEndprice(bidinfo.getPrice());
		auctioninfo.setStatus("竞价结束");
		auctioninfo.setPaystatus("未付款");
		auctioninfo.setAuctiontime(bidinfo.getAuctiontime());
		bidinfo.setStatus("竞价成功");
		auctionBidService.saveOrUpdate(bidinfo, BeanAdvertAuctionBidInfo.class);
		advertService.saveOrUpdate(auctioninfo, BeanAdvertAuctionInfo.class);
		return findAuctionDetail();
	}
	
	
	public String editAdvert(){
		id=this.getAdvert().getId();
		advertService.saveOrUpdate(this.getAdvert(), BeanAdvert.class);
		return findEditPage();
	}
	//删除广告位
	public String deleteAdvert(){
		advertService.delete(this.getId(), BeanAdvert.class);
		return findList();
	}
	
	//删除竞价信息
	public String deleteAuctionInfo(){
		id=this.getId();
		auctionService.delete(this.getAid(), BeanAdvertAuctionInfo.class);
		return findAdvertDetail();
	}
	//删除竞价明细信息
	public String deleteAuctionBidInfo(){
		aid=this.getAid();
		auctionService.delete(this.getBid(), BeanAdvertAuctionBidInfo.class);
		return findAuctionDetail();
	}
	public String deleteAll() {
		if (null != ids) {
			for (String idtemp : ids) {
				this.getBaseService().delete(Long.parseLong(idtemp), BeanAdvert.class);
			}
		}
		return findList();
	}
}
